package org.example.Utils;

import org.example.TankGan.Recorder;
import org.example.TankGan.pojo.Boom;
import org.example.TankGan.pojo.Bullet.Bullet;
import org.example.TankGan.pojo.Enemy.EnemyTank;
import org.example.TankGan.pojo.MyHeroTank.MyHero;
import org.example.TankGan.pojo.Tank;

import java.util.Vector;

/**
 * @author cc
 * @date 2021/4/19 15:37
 * @Description 判断是否被击中
 */

public class JydgeIsBoom {
    //敌方天纳克击毁
    public static Vector<Boom> JudgeIsBoomAram(Bullet bullet, EnemyTank enemyTank, Vector<Boom> booms) {
        switch (enemyTank.getDrection()) { //方向判断
            case 0:
            case 2:
                if (bullet.getX()-5 > enemyTank.getX() && enemyTank.getX() + 45 > bullet.getX() && bullet.getY() > enemyTank.getY()-5&& enemyTank.getY() + 65 > bullet.getY()) {
                    bullet.setLive(false);
                    enemyTank.setLive(false);
                    //创建boom，加入到boom集合
                    Boom boom = new Boom(enemyTank.getX() + 20, enemyTank.getY() + 30);
                    booms.add(boom);
                    Recorder.AddAllEnemy();
                }
                break;
            case 1:
            case 3:
                if (bullet.getX()-5 > enemyTank.getX() && bullet.getX() < enemyTank.getX() + 65 && bullet.getY() > enemyTank.getY()-5 && bullet.getY() < enemyTank.getY() + 45) {
                    bullet.setLive(false);
                    enemyTank.setLive(false);
                    Boom boom = new Boom(enemyTank.getX() + 30, enemyTank.getY() + 20);
                    booms.add(boom);
                    Recorder.AddAllEnemy();

                }
        }
        return booms;
    }
//我方坦克被击毁
    private static void setIsBoomandDispear(Tank tank,Bullet bullet,Vector<Boom> booms){
        switch (tank.getDrection()) { //方向判断
            case 0:
            case 2:
                if (bullet.getX() > tank.getX()-5 && bullet.getX()<tank.getX()+50&& bullet.getY() > tank.getY()-5 && tank.getY() + 70 > bullet.getY()) {
                    bullet.setLive(false);
//                    if (tank) 判断坦克是什么类型，如果是自己就转型后设置
//                    tank.setLive(false);
                    if (tank.getType()==0){
                        MyHero myHero= (MyHero)tank;
                        myHero.setIstrue(false);
                    }
                    //创建boom，加入到boom集合
                    Boom boom = new Boom(tank.getX() , tank.getY() );
                    booms.add(boom);
                }
                break;
            case 1:
            case 3:
                if (bullet.getX() > tank.getX()-5 && bullet.getX() < tank.getX() + 70 && bullet.getY() > tank.getY()-5 && bullet.getY() < tank.getY() + 50) {
                    bullet.setLive(false);
//                    tank.setLive(false);
                    if (tank.getType()==0){
                        MyHero myHero= (MyHero)tank;
                        myHero.setIstrue(false);
                    }
                    Boom boom = new Boom(tank.getX() , tank.getY() );

                    booms.add(boom);
                }
//                return booms;
        }
    }

//    public static Vector<Boom> JudgeIsBoom_Test(MyHero myHero, EnemyTank enemyTank, Vector<Boom> booms) {
//        if (myHero.getBullets() == null) {
//            return booms;
//        } else {
//            Vector<Bullet> bullets = myHero.getBullets();
//            for (int i = 0; i < bullets.size(); i++) {
//                switch (enemyTank.getDrection()) { //方向判断
//                    case 0:
//                    case 2:
//                        if (bullets.get(i).getX() > enemyTank.getX() && enemyTank.getX() + 40 > bullets.get(i).getX() && bullets.get(i).getY() > enemyTank.getY() && enemyTank.getY() + 60 > bullets.get(i).getY()) {
//                            bullets.get(i).setLive(false);
//                            enemyTank.setLive(false);
//                            //创建boom，加入到boom集合
//                            Boom boom = new Boom(enemyTank.getX() + 20, enemyTank.getY() + 30);
//                            booms.add(boom);
//                        }
//                        break;
//                    case 1:
//                    case 3:
//                        if (bullets.get(i).getX() > enemyTank.getX() && bullets.get(i).getX() < enemyTank.getX() + 60 && bullets.get(i).getY() > enemyTank.getY() && bullets.get(i).getY() < enemyTank.getY() + 40) {
//                            bullets.get(i).setLive(false);
//                            enemyTank.setLive(false);
//                            Boom boom = new Boom(enemyTank.getX() + 30, enemyTank.getY() + 20);
//                            booms.add(boom);
//                        }
//                }
//            }
//            return booms;
//        }
//    }

    public static Vector<Boom> JudgeAll(MyHero myHero, Vector<EnemyTank> enemyTanks, Vector<Boom> booms) {
        //判断玩家有几个子弹
        if (myHero.getBullets() == null) {
            //如果等于null继续判断单个子弹是否存在
            if (myHero.getBullet() != null && myHero.getBullet().isLive()) {//我的子弹是否存在
                for (int i = 0; i < enemyTanks.size(); i++) {
                    //判断敌人是否存活
                    if (enemyTanks.get(i).isLive()) {
                        JydgeIsBoom.JudgeIsBoomAram(myHero.getBullet(), enemyTanks.get(i), booms);
                    } else {
                        enemyTanks.remove(i);
                    }
                }
            }
        } else {
            for (int i = 0; i < enemyTanks.size(); i++) {
                //判断敌人是否存活
                if (enemyTanks.get(i).isLive()) {
                    //取出myhero的子弹
                    for (int j = 0; j < myHero.getBullets().size(); j++) {
                        //这个就是拿来判断是否爆炸（是否命中敌人，区域判断）
                        JydgeIsBoom.JudgeIsBoomAram(myHero.getBullets().get(j), enemyTanks.get(i), booms);
                    }
                } else {
                    enemyTanks.remove(i);
                }
            }
        }
        return booms;
    }


    public static void MyHeroisBomm(MyHero myHero, Vector<EnemyTank> enemyTanks, Vector<Boom> booms) {
        for (EnemyTank enemyTank : enemyTanks) {
            Vector<Bullet> bullets = enemyTank.getBullets();
            for (Bullet bullet : bullets) {
                if (bullet.isLive&&myHero.isIstrue()){
                    setIsBoomandDispear(myHero,bullet,booms);
                }
            }
        }
    }
}
